<?php
declare(strict_types=1);

namespace Service\AreaService;

use Service\BaseService;
use App\Model\BbcModel\Area;
use Service\Code\Normal;
use Service\CacheService\CacheProviderService as Cache;

/**
 * 区域服务中心
 * @author chenshubo
 */
class AreaService extends BaseService 
{
    /**
     * 查询区域数据
     * @param array $ids
     */
    public function queryAreaListById(array $ids = [])
    {
        $return = Area::instance()
            ->whereIn('id', $ids)
            ->get()
            ->toArray();
        return array_column($return, null, 'id');
    }

    /**
     * 获取区域数据
     * @param int $pid
     */
    public function getArea(int $pid = 0)
    {
        $list = Area::instance()
            ->where('pid', $pid)
            ->get()
            ->toArray();
        return Normal::returnCode(Normal::SUC, '获取成功', $list);
    }

    /**
     * 获取单个区域数据
     * @param int $pid
     */
    public function getAreaInfo(int $id = 0)
    {
        $info = Area::instance()->info(['id' => $id]);
        return Normal::returnCode(Normal::SUC, '获取成功', $info);
    }

    /**
     * 获取所有省市区
     * @param bool $is_tree true 是否返回树形
     */
    public static function getAreaByCache(bool $is_tree = false)
    {
        if ($is_tree == false) {
            $cache_key = 'Area_GetArea';
            $areas     = Cache::getCacheByKey($cache_key);
            if (empty($areas)) {
                $areas = Area::select(['id', 'name'])->get()->toArray();
                $areas = array_column($areas, null, 'id');
                Cache::cacheBySetKeyValue($cache_key, $areas, 86400);
            }
            return $areas;
        }
        $cache_key = 'Area_GetArea_Tree';
        $areas     = Cache::getCacheByKey($cache_key);
        if (empty($areas)) {
            $areas = Area::all()->toArray();
            $areas = tree($areas, 0, 'id', 'pid');
            Cache::cacheBySetKeyValue($cache_key, $areas, 86400);
        }
        return $areas;
    }

    /**
     * 获取所有地址递归列表
     */
    public static function getRegionTreeList()
    {
        $cache_key = "AlL_REGIONS";
        $regions   = Cache::getCacheByKey($cache_key);
        if (empty($regions)) {
            $regions = Area::select(['id', 'name', 'pid'])->get()->toArray();
            $regions = self::arrayKey($regions);
            Cache::cacheBySetKeyValue($cache_key, $regions, 604800);
        }
        return $regions;
    }

    /**
     * 递归所有地址成无限分类数组
     */
    public static function arrayKey($data, $region_id = 0)
    {
        $result = [];
        foreach ($data as $row) {
            if ($region_id == $row['pid']) {
                $key           = $row['id'];
                $row['childs'] = self::arrayKey($data, $row['id']);
                $result[$key]  = $row;
            }
        }
        return $result;
    }
    
    public function getAreaByIdcard($idcard) {
        $data=[
            'province_name'=>'',
            'province_id'=>0,
            'city_name'=>'',
            'city_id'=>0,
            'area_name'=>'',
            'area_id'=>0
        ];
        if(empty($idcard)){
           return $data; 
        }
        $province_code=str_pad(substr((string)$idcard, 0, 2), 6, '0', STR_PAD_RIGHT);
        $city_code=str_pad(substr((string)$idcard, 0, 4), 6, '0', STR_PAD_RIGHT);
        $area_code=str_pad(substr((string)$idcard, 0, 6), 6, '0', STR_PAD_RIGHT);
        $areas = Area::instance()
            ->whereIn('code', [$province_code,$city_code,$area_code])
            ->get()
            ->toArray();
        $areaMap=array_column($areas, null, 'code');
        $data=[
            'province_name'=>$areaMap[$province_code]['name']??'',
            'province_id'=>$areaMap[$province_code]['id']??0,
            'city_name'=>$areaMap[$city_code]['name']??'',
            'city_id'=>$areaMap[$city_code]['id']??0,
            'area_name'=>$areaMap[$area_code]['name']??'',
            'area_id'=>$areaMap[$area_code]['id']??0
        ];
        return $data;
    }
}
